package expertise.resume.impl;

import java.io.InputStream;
import java.util.Collection;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import expertise.resume.ResumeSkillParser;
import expertise.resume.ResumeStringifier;
import expertise.resume.SkillIdentifier;
import expertise.resume.SupportedResumeFormat;
import expertise.resume.exception.ResumeParsingException;
import expertise.resume.exception.ResumeStringifyException;
import expertise.resume.exception.UnsupportedResumeFormatException;
import expertise.resume.impl.stringify.PdfResumeStringifier;

@Component
public class ResumeSkillParserImpl implements ResumeSkillParser {

	private static final Logger log = Logger.getLogger(ResumeSkillParserImpl.class);

	@Resource(type = PdfResumeStringifier.class)
	private ResumeStringifier pdfStringifier;

	@Resource
	private SkillIdentifier skillIdentifier;

	@Override
	public Collection<String> parseSkillsFromResume(final SupportedResumeFormat format,
			final InputStream inputStream) throws ResumeParsingException {
		log.debug("Parsing skills from a resume in " + format.toString() + " format");
		String raw = stringify(format, inputStream);
		if (raw == null || raw.isEmpty()) {
			log.error("Issue with stringification of resume, no content was found");
			throw new ResumeStringifyException("Error getting raw text data from the resume");
		}
		return skillIdentifier.identifySkills(raw);
	}

	private String stringify(final SupportedResumeFormat format, final InputStream inputStream)
			throws ResumeStringifyException, UnsupportedResumeFormatException {
		final String rawResumeText;
		switch (format) {
			case PDF:
				log.debug("Format identified as PDF, going to stringify");
				rawResumeText = pdfStringifier.stringifyResume(inputStream);
				break;
			default:
				log.error("Unsupported resume format: " + format);
				throw new UnsupportedResumeFormatException("Unsupported resume format: " + format);
		}
		log.debug("Content: \n\n" + rawResumeText + "\n\n");
		return rawResumeText;
	}
}
